package cn.mayaslight.common.http;

import org.apache.http.client.methods.RequestBuilder;

import cn.mayaslight.common.chain.IChain;
import cn.mayaslight.common.exception.HandleException;
import cn.mayaslight.common.springbean.APICaller;

public class HttpMethodJudgement<T> extends IChain{
	
	private RequestBuilderPack<T> requestBuilder;
	private StringBuilder stringBuilder;
	
	public HttpMethodJudgement(RequestBuilderPack<T> requestBuilder, StringBuilder stringBuilder) {
		this.requestBuilder = requestBuilder;
		this.stringBuilder = stringBuilder;
	}

	@Override
	public boolean doThis() throws Exception {
		if(stringBuilder.indexOf(APICaller.HTTP_URL_GET_PREFIX) < 0
			&& stringBuilder.indexOf(APICaller.HTTP_URL_POST_PREFIX) < 0
			&& stringBuilder.indexOf(APICaller.HTTP_URL_JSON_PREFIX) < 0
			&& stringBuilder.indexOf(APICaller.HTTP_URL_XML_PREFIX) < 0
		)
			return false;

		if(stringBuilder.toString().startsWith(APICaller.HTTP_URL_GET_PREFIX)){
			stringBuilder.delete(0, APICaller.HTTP_URL_GET_PREFIX.length());
			requestBuilder.setRequestBuilder(RequestBuilder.get());
		}else if(stringBuilder.toString().startsWith(APICaller.HTTP_URL_POST_PREFIX)){
			stringBuilder.delete(0, APICaller.HTTP_URL_POST_PREFIX.length());
			requestBuilder.setRequestBuilder(RequestBuilder.post());
		}else{
			throw new HandleException("ApiKey对应的连接不为GET或者POST!");
		}
		
		return true;
		
	}

	@Override
	public void doIfCompleteThis() throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFinal() throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doIfCompleteAll() throws Exception {
		// TODO Auto-generated method stub
		
	}

}
